package com.pengtu.web.app;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.entity.app.Authority;
import com.pengtu.service.app.AccountManager;
import com.pengtu.utils.web.Struts2Utils;
import com.pengtu.web.BaseActionSupport;

@Namespace("/account")
@Results({ @Result(name = BaseActionSupport.RELOAD, location = "authority.action", type = "redirect") })
public class AuthorityAction extends BaseActionSupport<Authority> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private AccountManager accountManager;

	private Authority entity;
	private String id;
	private List<Authority> authorityList;
	private Page<Authority> page = new Page<Authority>(10);// 每页5条记录

	@Autowired
	public void setAccountManager(AccountManager accountManager) {
		this.accountManager = accountManager;
	}

	public Authority getEntity() {
		return entity;
	}

	public void setEntity(Authority entity) {
		this.entity = entity;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<Authority> getAuthorityList() {
		return authorityList;
	}

	public void setAuthorityList(List<Authority> authorityList) {
		this.authorityList = authorityList;
	}

	public Page<Authority> getPage() {
		return page;
	}

	public void setPage(Page<Authority> page) {
		this.page = page;
	}

	@Override
	public Authority getModel() {
		// TODO Auto-generated method stub
		return entity;
	}

	@Override
	public String list() throws Exception {
		// TODO Auto-generated method stub
		QueryFilter filters = QueryFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
		// 设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.DESC);
		}
		page = accountManager.searchAuthority(page, filters);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		// TODO Auto-generated method stub
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		// TODO Auto-generated method stub
		accountManager.saveAuthority(entity);
		addActionMessage("保存权限成功");
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		// TODO Auto-generated method stub
		accountManager.delete(id);
		addActionMessage("删除权限成功");
		return RELOAD;
	}

	@Override
	protected void prepareModel() throws Exception {
		// TODO Auto-generated method stub
		if (StringUtils.isEmpty(id)) {
			entity = new Authority();
		} else {
			entity = accountManager.getAuthorityById(id);
		}
	}

}
